import store from '../store/index.js'

let url=process.env.NODE_ENV ==='development'?'http://localhost:8082':'https://course.xiejr.com:444/api';

const http={
	get:function(uri,data,loading = true){
		if(loading){
			uni.showLoading()
		}
		let header={
			
		};
		let userinfo=uni.getStorageSync('userinfo');
		
		return new Promise((reslove,reject)=>{
			uni.request({
				url:`${url}/${uri}`,
				data:data,
				dataType:'json',
				method:'GET',
				header:header,
				success: (res) => {
					if(res.data.code==='200'){
						reslove(res.data.data);
					}else{
						reject(res.data);
					}
					
				},
				fail:(err)=>{
					reject(err)
				},
				complete() {
					uni.hideLoading()
				}
			})
		})
	},
	post:function(uri,data,loading=true){
		if(loading){
			uni.showLoading()
		}
		let header={
			'content-type': 'application/json'
		};
		if(store.getters.TOKEN!==''){
			header.token=store.getters.TOKEN;
		}
		return new Promise((reslove,reject)=>{
			uni.request({
				url:`${url}/${uri}`,
				header:header,
				method:'POST',
				data:JSON.stringify(data),
				dataType:'json',
				success:res=>{
					if(res.data.code==='200'){
						reslove(res.data.data);
					}else{
						reject(res.data)
					}
				},
				fail:err=>{
					reject(err)
				},
				complete(){
					uni.hideLoading()
				}
				
			})
		})
	}
	
}


export default {
	
	install:function(Vue){
		if(Vue){
			Vue.prototype.$http=http;
		}
	}
	
}